<?php

	//zaczytuje plik xml'owy i produkuje tablice
	//
	// - usuniete problemy z pamiecia

    class CXmlToArray
    {

		var $szXmlFile = '';		//content pliku xml
		var $hParser;				//uchwyt do parsera
			
		var $aTmp = array();	
		var $aOut = array();		//tablica z rezultatem dzialania parsera
		
	
		static function &GetInstance()
		{
		    global $g_hObjects;
								
			$m_hApp       = new CXmlToArray();
			$g_hObjects[] = $m_hApp;
			$m_hApp->OnInit();
		    	
		    return $m_hApp;	
		}
			
		function OnInit()
		{	
		}
		
		function OnDestroy()
		{	
		}
	
		function Parse($szFilePath)
		{
			//ladujemy plik xml
		    $this->szXmlFile = CFile::FileToString($szFilePath);
				
		   	//wywal blad jesli nie zaladowano
		    if(!$this->szXmlFile)
				return false;
	
			//obcinamy znaki specjalne
			$this->szXmlFile = trim($this->szXmlFile);
				
			//inicjacja parsera i przypisanie go do obiektu tej klasy
			$this->hParser = xml_parser_create();
		    xml_set_object($this->hParser,$this);
				
		    //ustawiamy opcje parsera 
		    xml_parser_set_option($this->hParser,XML_OPTION_CASE_FOLDING,false);
		    xml_parser_set_option($this->hParser,XML_OPTION_TARGET_ENCODING,'UTF-8');
		    xml_parser_set_option($this->hParser,XML_OPTION_SKIP_WHITE,0);
		    xml_set_element_handler($this->hParser,'OnTagOpen','OnTagClose');
	
		    //handler do tagu
		    xml_set_character_data_handler($this->hParser,'OnTagData');
			
		    //odpalamy parsera
		    $szXmlData = xml_parse($this->hParser,$this->szXmlFile);
	
		    if(!$szXmlData)
		    {
				echo "Blad w parsowaniu XML! <br/><br/>".xml_error_string(xml_get_error_code($this->hParser));
				echo "Linia ".xml_get_current_line_number($this->hParser);
	
				die();
		    }
		    
		    //zwalniamy parser
		    xml_parser_free($this->hParser);
		    
		    return $this->aTmp[0];
		}
	
		
		function OnTagOpen($hParser,$szName,$aAttr)
		{
		    $aTag[$szName] = array();
	
		    foreach($aAttr as $szKey => &$szVal)
		    {
				$aTag[$szName][$szKey] = $szVal;
		    }
	
		    array_push($this->aTmp,$aTag);
		}
			
		function OnTagData($hParser,$szTagData)
		{
		    $szTagData = trim($szTagData);
				
		    if($szTagData == '') { return; }
	
	
		    $szKey = key($this->aTmp[count($this->aTmp)-1]);

		    
		    //crazy but fixes wired splitting bug
		    if( isset($this->aTmp[count($this->aTmp)-2][$szKey]) )
		    {	
				$this->aTmp[count($this->aTmp)-2][$szKey] .= '|'. $szTagData;
		    }
		    else
		    {
				$this->aTmp[count($this->aTmp)-2][$szKey] = $szTagData;
		    }
		}
	
		
		function OnTagClose($hParser,$szName) 
		{	
		    if( count($this->aTmp) == 0) { return; }
		
		    $szKey = key($this->aTmp[count($this->aTmp)-1]);
		    
		    $szTxt = '';
		    
		    if(isset($this->aTmp[count($this->aTmp)-2][$szKey]))
		    {
				$szTxt = $this->aTmp[count($this->aTmp)-2][$szKey];
			    
				//var_dump($this->aTmp[count($this->aTmp)-2][$szKey]);
				//echo "<br/><br/>";
		    }
		    
		    
		    //dependencies
		    $aDeps = $this->aTmp[count($this->aTmp)-1][$szKey];
	
		    unset($this->aTmp[count($this->aTmp)-1][$szKey]);
	
		    
		    if($aDeps)
		    {
				$this->aTmp[count($this->aTmp)-1] = array_merge($this->aTmp[count($this->aTmp)-1],$aDeps);
		    }
		    
		    if($szTxt)
		    {
				$this->aTmp[count($this->aTmp)-1] = $szTxt;
		    }
	
		    $this->aTmp[count($this->aTmp)-2][$szKey] = $this->aTmp[count($this->aTmp)-1];
		    
		    				
		    	if(count($this->aTmp))
				array_pop($this->aTmp);	
		}
    }	
	
?>